/* $Header: p_config.c,v 2.2 01/10/02 10:27:47 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/* Inspected 2/6/92, DeGood Freed Przybylski Wikfors                   */

/***********************************************************************/
/* MODULE NAME:  Reconfigure                                           */
/* ============                                                        */
/*                                                                     */
/* Author:  wikfors                                                    */
/*                                                                     */
/* MODULE PURPOSE:  called when column or inlet parms (source,len,diam,*/
/* gas) change to set column and inlet define flags and rebuild consts */
/* used for calculating flows, pressures, etc.                         */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <error.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <p_status.h>
#include <list_mgmt.h>
#include <zone_config.h>
#include <ramps.h>         /* needed for status.h */
#include <zone_status.h>
#include <status.h>  /* pull in Initialization Complete */
#include <detector.h>
#include <keyboard.h>

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/

#include "proto.h"
#include "p_err.h"

/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/


/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/


/*****************************/
/*   LOCAL DEFINES           */
/*****************************/


/*****************************/
/*   STATIC VARIABLES        */
/*****************************/


/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/
/**begin_proto**/

#pragma SECTION PROG=rom3

/**end_proto**/

/***********************************************************************/
/* FUNCTION: BuildConstIfGasKnown                                      */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: If the source's gas is known, set the column defined flag  */
/*          and build the constants                                    */
/*                                                                     */
/* GLOBALS USED:  Inst_Status, Inst_Config                             */
/*                                                                     */
/***********************************************************************/



void BuildConstIfGasKnown( pCOLUMN_STATUS status, GAS_TYPE gas_type, U_INT16 dcm2_per_iu )               

{
                                                                        
        if ( gas_type == UNKNOWN_GAS )                                  
        {                                                               
             status->Config->Defined = FALSE;                           
        }                                                               
        else                                                            
        {                                                               
             status->Config->Defined = TRUE;                            
             status->Dcm2_Per_IU = dcm2_per_iu;                         
             status->C1 = BuildC1( status, dcm2_per_iu, gas_type );  /* we need outlet velocity for method translation */    
             status->C2 = BuildC2( status, dcm2_per_iu, gas_type );  /* avg linear velocity */                      
             status->C3 = BuildC3( status, dcm2_per_iu, gas_type );  /* volumetric flow */
             /*status->C4 = BuildC4( status->C3 );   we don't do mass flow */                     
        }                                                               

}


/***********************************************************************/
/* FUNCTION: DefinedColumn                                             */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: to tell us if we know all we can about the column.         */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

BIT8 DefinedColumn( pCOLUMN_STATUS status ) 

/**end_proto**/
{
    return  status->Config->Defined;
}

/***********************************************************************/
/* FUNCTION: ConfigureColumn                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  build column constants if column is defined.              */
/*           mark the column as defined or undefined.                  */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

void ConfigureColumn( pCOLUMN_STATUS status )

/**end_proto**/
{

   GAS_TYPE gas_type;
   U_INT16  dcm2_per_iu;

   /* to be defined, a column needs a length, diameter, source connection, and a known carrier gas */

       if (( status->Config->Length == 0 )                ||
           ( status->Config->Diam   == 0 )                ||
           ( status->Config->Source == UNKNOWN_SOURCE ))
       {
           status->Config->Defined = FALSE;

           switch ( status->Config->Source )
           {
           case FRNT_INLET: status->Gas_Type = Inst_Config.Pneu.Frnt_Inlet.Gas_Type;
                            break;

           case BACK_INLET: status->Gas_Type = Inst_Config.Pneu.Back_Inlet.Gas_Type;
                            break;

           case AUX_3:      status->Gas_Type = Inst_Config.Pneu.Aux_3.Gas_Type;
                            break;

           case AUX_4:      status->Gas_Type = Inst_Config.Pneu.Aux_4.Gas_Type; 
                            break;

           case AUX_5:      status->Gas_Type = Inst_Config.Pneu.Aux_5.Gas_Type; 
                            break;

           default:  /* don't touch gas type, we don't know what it is, (source == unknown)  */   ;

           } /* switch */
       }
       else
       {
           switch ( status->Config->Source )
           {
           case FRNT_INLET: dcm2_per_iu = Inst_Status.Pneu.Frnt_Inlet.Dcm2_Per_IU;
                            gas_type    = Inst_Config.Pneu.Frnt_Inlet.Gas_Type;
                            status->Gas_Type = gas_type;
                            BuildConstIfGasKnown( status, gas_type, dcm2_per_iu );

                            break;

           case BACK_INLET: dcm2_per_iu = Inst_Status.Pneu.Back_Inlet.Dcm2_Per_IU;
                            gas_type    = Inst_Config.Pneu.Back_Inlet.Gas_Type;
                            status->Gas_Type = gas_type;

                            BuildConstIfGasKnown( status, gas_type, dcm2_per_iu );

                            break;


           case AUX_3:      if ( Inst_Config.Pneu.Aux_3.Functional )
                            {
                                dcm2_per_iu = Inst_Status.Pneu.Aux.Aux_3.Dcm2_Per_IU;
                                gas_type    = Inst_Config.Pneu.Aux_3.Gas_Type;
                                status->Gas_Type = gas_type;
    
                                BuildConstIfGasKnown( status, gas_type, dcm2_per_iu );
                            }
                            else
                            {
                                 /* if we don't have a proportional valve, we cant control */
                                 status->Config->Defined = FALSE;
                            }
                            break;

           case AUX_4:      if ( Inst_Config.Pneu.Aux_4.Functional )
                            {
                                dcm2_per_iu = Inst_Status.Pneu.Aux.Aux_4.Dcm2_Per_IU;
                                gas_type    = Inst_Config.Pneu.Aux_4.Gas_Type;
                                status->Gas_Type = gas_type;
    
                                BuildConstIfGasKnown( status, gas_type, dcm2_per_iu );
                            }
                            else
                            {
                                 /* if we don't have a proportional valve, we cant control */
                                 status->Config->Defined = FALSE;
                            }
                            break;

           case AUX_5:      if ( Inst_Config.Pneu.Aux_5.Functional )
                            {
                                dcm2_per_iu = Inst_Status.Pneu.Aux.Aux_5.Dcm2_Per_IU;
                                gas_type    = Inst_Config.Pneu.Aux_5.Gas_Type;
                                status->Gas_Type = gas_type;
    
                                BuildConstIfGasKnown( status, gas_type, dcm2_per_iu );
                            }
                            else
                            {
                                 /* if we don't have a proportional valve, we cant control */
                                 status->Config->Defined = FALSE;
                            }
                            break;

           default: PneuFatalError( 0, BAD_COLUMN_SOURCE ) ;
           } /* switch */
       }


}

/***********************************************************************/
/* FUNCTION: AllColumnsOnThisSourceDefined()                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: to verify that all columns on this source are defined      */
/*                                                                     */
/* RETURNED VALUE: TRUE if all columns on this source are defined      */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

BIT8 AllColumnsOnThisSourceDefined( SOURCE_CONNECTION source )

/**end_proto**/
{

     /* if either column is on this source and defined */
   
      if  (      /* column 1 is on this source and is defined */
                (( Inst_Config.Pneu.Column_1.Source  == source) &&
                 ( Inst_Config.Pneu.Column_1.Defined == TRUE      ))
           ||
                    /* column 2 is on this source and is defined */
                (( Inst_Config.Pneu.Column_2.Source  == source) &&
                 ( Inst_Config.Pneu.Column_2.Defined == TRUE      ))   )
      {

          /**  at least one column is on this source and defined, check for both **/

          if (      /* both columns are on this source  */
                (( Inst_Config.Pneu.Column_1.Source  == source) &&
                 ( Inst_Config.Pneu.Column_2.Source  == source))
             &&
                    /* either of them are  undefined    */
                (( Inst_Config.Pneu.Column_1.Defined == FALSE ) ||
                 ( Inst_Config.Pneu.Column_2.Defined == FALSE ))     )
          {
               return FALSE;  /* two columns at least one is undefined */
          }
          else
          {
               return TRUE;   /* all columns are defined */

          }
      }
      else
      {
           return FALSE;  /* neither columns is defined or connected to this source */
      }
                                                                          


}


/***********************************************************************/
/* FUNCTION: AllColumnsOnThisOutletDefined()                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: to verify that all columns on this outlet are defined      */
/*                                                                     */
/* RETURNED VALUE: TRUE if all columns on this outlet are defined      */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

BIT8 AllColumnsOnThisOutletDefined( OUTLET_CONNECTION outlet )

/**end_proto**/
{

     /* if either column is on this outlet and defined */
   
      if  (      /* column 1 is on this outlet and is defined */
                (( Inst_Config.Pneu.Column_1.Outlet  == outlet) &&
                 ( Inst_Config.Pneu.Column_1.Defined == TRUE      ))
           ||
                    /* column 2 is on this outlet and is defined */
                (( Inst_Config.Pneu.Column_2.Outlet  == outlet) &&
                 ( Inst_Config.Pneu.Column_2.Defined == TRUE      ))   )
      {

          /**  at least one column is on this outlet and defined, check for both **/

          if (      /* both columns are on this outlet  */
                (( Inst_Config.Pneu.Column_1.Outlet  == outlet) &&
                 ( Inst_Config.Pneu.Column_2.Outlet  == outlet))
             &&
                    /* either of them are  undefined    */
                (( Inst_Config.Pneu.Column_1.Defined == FALSE ) ||
                 ( Inst_Config.Pneu.Column_2.Defined == FALSE ))     )
          {
               return FALSE;  /* two columns at least one is undefined */
          }
          else
          {
               return TRUE;   /* all columns are defined */

          }
      }
      else
      {
           return FALSE;  /* neither columns is defined or connected to this source */
      }
                                                                          
}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

BIT8 SourceControlledByColumn ( SOURCE_CONNECTION source )

/**end_proto**/
{

    return  (( Inst_Config.Pneu.Column_1.Source == source ) ||
             ( Inst_Config.Pneu.Column_2.Source == source ));

}

/***********************************************************************/
/* FUNCTION:  ReconfigurePneu()                                        */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:1 to set up the bit flags is inst_status indicating that    */
/*           all of the parameters needed to control a column are      */
/*           known and the flags indicating that all columns connected */
/*           to an inlet or aux are defined.                           */
/*                                                                     */
/*         2 if the column parms are known, build the constants needed */
/*           for pneumatic display and control.                        */
/*                                                                     */
/*         3 copy pressure sensor internal unit scaling value from the */
/*           inlet config to the column status.  this will be used by  */
/*           the inlet control and display functions                   */
/*                                                                     */
/* GLOBALS USED:   Inst_Config, Inst_Status                            */
/*                                                                     */
/*                                                                     */
/* CALLED:   Once at startup, and when any of the column configuration */
/*           parameters (len, diam, source, source->gas) are changed.  */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

void ReconfigurePneu(void)

/**end_proto**/
{

       BIT8 col1_was_defined;
       BIT8 col2_was_defined;
       BIT8 col1_is_defined;
       BIT8 col2_is_defined;

       col1_was_defined = DefinedColumn( &Inst_Status.Pneu.Column_1 );
       col2_was_defined = DefinedColumn( &Inst_Status.Pneu.Column_2 );

       ConfigureColumn( &Inst_Status.Pneu.Column_1 );
       ConfigureColumn( &Inst_Status.Pneu.Column_2 );

       col1_is_defined = DefinedColumn( &Inst_Status.Pneu.Column_1 );
       col2_is_defined = DefinedColumn( &Inst_Status.Pneu.Column_2 );


       Inst_Status.Pneu.Frnt_Inlet.Has_Column      = AllColumnsOnThisSourceDefined(FRNT_INLET);
       Inst_Status.Pneu.Frnt_Inlet.Controlled_By_Column =
                                                     SourceControlledByColumn( FRNT_INLET );

       Inst_Status.Pneu.Back_Inlet.Has_Column      = AllColumnsOnThisSourceDefined(BACK_INLET);
       Inst_Status.Pneu.Back_Inlet.Controlled_By_Column =
                                                     SourceControlledByColumn( BACK_INLET );

       Inst_Status.Pneu.Frnt_Detector.Has_Column   = AllColumnsOnThisOutletDefined( FRNT_DET );

       Inst_Status.Pneu.Back_Detector.Has_Column   = AllColumnsOnThisOutletDefined( BACK_DET );

       Inst_Status.Pneu.Aux.Aux_3.Has_Column           = AllColumnsOnThisSourceDefined(AUX_3);
       Inst_Status.Pneu.Aux.Aux_3.Controlled_By_Column = SourceControlledByColumn( AUX_3 );

       Inst_Status.Pneu.Aux.Aux_4.Has_Column           = AllColumnsOnThisSourceDefined(AUX_4);
       Inst_Status.Pneu.Aux.Aux_4.Controlled_By_Column = SourceControlledByColumn( AUX_4 );

       Inst_Status.Pneu.Aux.Aux_5.Has_Column           = AllColumnsOnThisSourceDefined(AUX_5);
       Inst_Status.Pneu.Aux.Aux_5.Controlled_By_Column = SourceControlledByColumn( AUX_5 );


       if ( Initialization_Complete == TRUE )
       {
           if ( !col1_was_defined && col1_is_defined )
           {
               NewlyDefinedColumn( &Inst_Status.Pneu.Column_1 );
           }
           else if ( col1_was_defined && !col1_is_defined )
           {
               NewlyUnDefinedColumn( &Inst_Status.Pneu.Column_1 );
           }

           if ( !col2_was_defined && col2_is_defined )
           {
               NewlyDefinedColumn( &Inst_Status.Pneu.Column_2 );
           }
           else if ( col2_was_defined && !col2_is_defined )
           {
               NewlyUnDefinedColumn( &Inst_Status.Pneu.Column_2 );
           }
       }
    
       /* any time the gas type to a detector changes, (including carrier) */
       /* the det driver needs to be told.  these calls will take care of  */
       /* any changes from column connections or carrier gas type changes. */

       DetGasTypeChanged( FRONT );
       DetGasTypeChanged( BACK  );

       if (( Inst_Status.Pneu.Frnt_Detector.Has_Column == FALSE ) &&
           ( Active_Wkfile.Pneu.Frnt_Detector.Constant_Total_Makeup == ON ))
       {
             Active_Wkfile.Pneu.Frnt_Detector.Constant_Total_Makeup = OFF;
       }

       if (( Inst_Status.Pneu.Back_Detector.Has_Column == FALSE ) &&
           ( Active_Wkfile.Pneu.Back_Detector.Constant_Total_Makeup == ON ))
       {
             Active_Wkfile.Pneu.Back_Detector.Constant_Total_Makeup = OFF;
       }

       if ( !DoingMethodInstall() )
       {
            RangeCheckColumnRamps( &Inst_Status.Pneu.Column_1 );
            RangeCheckColumnRamps( &Inst_Status.Pneu.Column_2 );
       }

}

/**begin_proto**/

#pragma SECTION UNDO

/**end_proto**/
